from django import forms
from django.core.exceptions import ValidationError
from user.models import VSession,User,WarZone,Company
from need.models import SoftwareNeed
from software.models import Software,SoftwareVersion
from storehouse.models import Storehouse
from announcement.models import Announcement
import pdb


def token_check(value):
    # 验证token必须存在
    sessionList = VSession.objects.filter(token=value)
    if not sessionList.exists():
        errMsg = 'token Error'
        isValidOk = False
        raise ValidationError(errMsg)
    else:
        session = sessionList[0]



def softwareId_check(value):
    # 验证战区id必须存在
    if value != -1:
        is_num_by_except(value)
        software = Software.objects.filter(pk=value)
        if not software.exists():
            errMsg = 'softwareId Error'
            raise ValidationError(errMsg)

def softwareVersionId_check(value):
    # 验证战区id必须存在
    if value != -1:
        is_num_by_except(value)
        softwareVersion = SoftwareVersion.objects.filter(pk=value)
        if not softwareVersion.exists():
            errMsg = 'softwareVersionId Error'
            raise ValidationError(errMsg)

def needId_check(value):
    # 验证需求id必须存在
    if value != -1:
        is_num_by_except(value)
        need = SoftwareNeed.objects.filter(pk=value)
        if not need.exists():
            errMsg = 'needId Error'
            raise ValidationError(errMsg)

def maxItem_check(value):
    if value < 1:
        errMsg = 'maxItem Error'
        raise ValidationError(errMsg)


def page_check(value):
    if value < 1:
        errMsg = 'page Error'
        raise ValidationError(errMsg)

def warZoneId_check(value):
    # 验证战区id必须存在
    if value != -1:
        is_num_by_except(value)
        warZone = WarZone.objects.filter(pk=value)
        if not warZone.exists():
            errMsg = 'warZoneId Error'
            raise ValidationError(errMsg)


def companyId_check(value):
    # 验证单位id必须存在
    if value != -1:
        is_num_by_except(value)
        company = Company.objects.filter(pk=value)
        if not company.exists():
            errMsg = 'companyId Error'
            raise ValidationError(errMsg)

def storehouseId_check(value):
    # 验证仓库id必须存在
    if value != -1:
        is_num_by_except(value)
        storehouse = Storehouse.objects.filter(pk=value)
        if not storehouse.exists():
            errMsg = 'storehouseId Error'
            raise ValidationError(errMsg)

def is_num_by_except(num):
    try:
        int(num)
        return True
    except ValueError:
        errMsg = 'value not int'
        raise ValidationError(errMsg)

def announcementId_check(value):
    # 验证软件id必须存在
    if value != -1:
        is_num_by_except(value)
        anno = Announcement.objects.filter(pk=value)
        if not anno.exists():
            errMsg = 'annoId Error'
            raise ValidationError(errMsg)

class AddNeedForm(forms.Form):
    title = forms.CharField(max_length=20)
    needContent = forms.CharField(max_length=10000)
    token = forms.CharField(max_length=100,validators=[token_check])
    softwareVersionId = forms.IntegerField(validators=[softwareVersionId_check])


class GetNeedListForm(forms.Form):
    token = forms.CharField(max_length=100,validators=[token_check])
    maxItem = forms.IntegerField(validators=[maxItem_check])
    page = forms.IntegerField(validators=[page_check])
    softwareVersionId = forms.IntegerField(validators=[softwareVersionId_check],required=False)
    needState = forms.CharField(max_length=20)

class NeedOptionForm(forms.Form):
    token = forms.CharField(max_length=100,validators=[token_check])
    needId = forms.IntegerField(validators=[needId_check])
    option = forms.CharField(max_length=20)
    verify = forms.CharField(max_length=20,required=False)   
    result = forms.CharField(max_length=500,required=False) 

class NeedScreenForm(forms.Form):
    token = forms.CharField(max_length=100,validators=[token_check])
    maxItem = forms.IntegerField(validators=[maxItem_check])
    page = forms.IntegerField(validators=[page_check])
    warZoneId = forms.IntegerField(validators=[warZoneId_check],required=False)
    companyId = forms.IntegerField(validators=[companyId_check],required=False)
    storehouseId = forms.IntegerField(validators=[storehouseId_check],required=False)
    needState = forms.CharField(max_length=20)
    key = forms.CharField(max_length=20,required=False)     

class NeedAnnounceForm(forms.Form):
    token = forms.CharField(max_length=100,validators=[token_check])
    noticeId = forms.IntegerField(validators=[announcementId_check])
    # maxItem = forms.IntegerField(validators=[maxItem_check])
    # page = forms.IntegerField(validators=[page_check])